import java.util.Random;
import java.util.Scanner;

public class homeWork {

    public static void main(String[] args) {

//        (1)在main方法中启动两个线程
//        (2)第1个线程循环随机打印100以内的整数
//        (3)直到第2个线程从键盘读取了“Q”命令
        Random a=new Random();
        Thread a1=new Thread(a);
        a1.setName("第一个线程");
        a1.start();
        Control c=new Control(a);
        new Thread(c).start();

    }

    public static class Control implements Runnable{
        Scanner sc=new Scanner(System.in);
        Random random;

        public Control(Random random){
            this.random=random;
        }

        @Override
        public void run() {
            while (true){
                System.out.println("输入Q停止");
                String q=sc.next();
                if (q.equals("Q")){
                    random.setFlag(false);
                    System.out.println("成功退出了");
                    break;
                }
            }
        }
    }

    public static class Random implements Runnable{

        boolean flag=true;
        public void setFlag(boolean flag){
            this.flag=flag;
        }

        public void run(){
            synchronized (this){
                while (flag){
                    System.out.println(Thread.currentThread().getName()+":"+(int)(Math.random()*101));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }


}
